<template>
  <div>
    <el-card>
      <el-form ref="query" :model="query" label-width="80px" :inline="true">
        <el-form-item>
          <el-input
            v-model="query.keyword"
            placeholder="请输入内容"
            class="input-with-select"
          >
            <el-button slot="append" icon="el-icon-search" />
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="add">新增</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="dataList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#" />
        <el-table-column prop="title" label="专栏标题" />
        <el-table-column prop="cover_url" label="专栏封面">
          <template slot-scope="scope">
            <div class="video_img">
              <img :src="scope.row.cover_url">
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="desc" label="专栏介绍" />
        <el-table-column label="所属分类" >
          <template slot-scope="scope">
            {{scope.row.categoryInfo.name}}
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="创建时间" />
        <el-table-column>
          <template slot-scope="scope">
            <!-- <el-button type="text" @click="ev_relatedVideo(scope.row)">编辑</el-button> -->
            <el-button type="text" @click="ev_relatedVideo(scope.row)">关联视频</el-button>
            <el-button type="text" @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :current-page="query.page"
        :page-size="query.pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <el-dialog :visible.sync="formVisible" :title="formTitle">
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="100px"
      >
        <el-form-item prop="title" label="专栏标题">
          <el-input
            v-model="formData.title"
            placeholder="请输入专栏标题"
            type="text"
          />
        </el-form-item>
        <el-form-item prop="cover_url" label="封面图">
          <ImageUpload
            ref="imageupload"
            :init-image-url="formData.cover_url"
            @choose-image="uploadImage"
          />
        </el-form-item>
        <el-form-item prop="desc" label="专栏介绍">
          <el-input
            v-model="formData.desc"
            placeholder="请输入专栏介绍"
            type="text"
          />
        </el-form-item>
        <el-form-item prop="category_id" label="所属科室">
          {{ formExtra.category_id }}
          <el-button @click="openCategory">选择</el-button>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeForm">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog :visible.sync="categoryVisible" title="选择科室">
      <div>
        <el-tree
          :data="categoryTree"
          :props="defaultProps"
          node-key="id"
          default-expand-all
          :expand-on-click-node="false"
        >
          <span slot-scope="{ node, data }" class="custom-tree-node">
            <span>{{ node.label }}</span>
            <span>
              <el-button
                size="mini"
                @click="() => chooseCategory(data)"
              >选择</el-button>
            </span>
          </span>
        </el-tree>
      </div>
    </el-dialog>

    <RelationVideo @getDataList="getDataList" :relationVideoData="relationVideoData" :relationVideoVisible="relationVideoVisible" @closeRelationModal="value => relationVideoVisible=value"/>
  </div>
</template>

<script>
import RelationVideo from './components/relationVideo'
import ImageUpload from '@/components/ImageUpload/index'
import { getTopicList, createTopic, updateTopic, deleteTopic,videoList,videoRelieve,videoRelation,videoFree } from '@/api/topic/index'
import { getCategoryTree } from '@/api/category/index'
export default {
  components: {
    ImageUpload,
    RelationVideo
  },
  data() {
    return {
      id: 0,
      relationVideoVisible:false,
      relationVideoData:{
        id:'',
        list:[]
      },
      query: {
        page: 1,
        pageSize: 10,
        keyword: ''
      },
      dataList: [],
      total: 0,

      formTitle: '新增专栏',
      formVisible: false,
      formData: {
        title: '',
        cover_url: '',
        desc: '',
        category_id: 0
      },
      formExtra: {
        category_id: ''
      },
      formRules: {
        title: {
          required: true,
          message: '请输入专栏标题',
          trigger: 'blur'
        },
        cover_url: {
          required: true,
          message: '上传专栏封面图',
          trigger: 'blur'
        },
        category_id: {
          min: 1,
          message: '请选择分类',
          trigger: 'blur',
          type: 'number'
        }
      },

      categoryTree: [],
      defaultProps: {
        children: 'sub',
        label: 'name'
      },
      categoryVisible: false
    }
  },
  created() {
    this.getDataList()

    //117
    // this.ev_videoRelieve()


    // this.ev_videoFree()
    
  },
  methods: {
    // // 设置免费视频
    // ev_videoFree(){
    //   videoFree({"video_id": 197,is_free:1},30)
    // },
    //栏目增加单个关联视频
    // ev_videoRelation(){
    //   videoRelation({"video_id": 197},30)
    // },
    //删除单个关联视频
    // ev_videoRelieve(){
    //   videoRelieve({"video_id": 138},30)
    // },
    //点击关联视频
    async ev_relatedVideo(item){
      this.relationVideoData.id=item.id;
      this.relationVideoVisible=true;
    },
    async getDataList() {
      const { data: res } = await getTopicList(this.query)
      this.dataList = res.list
      this.total = res.total
    },
    handleCurrentChange(page) {
      this.query.page = page
      this.getDataList()
    },
    closeForm() {
      this.formVisible = false
    },
    add() {
      this.formVisible = true
    },
    async openCategory() {
      this.categoryVisible = true
      const { data: res } = await getCategoryTree()
      this.categoryTree = res
    },
    chooseCategory(data) {
      this.formData.category_id = data.id
      this.formExtra.category_id = data.name
      this.categoryVisible = false
    },
    uploadImage(data) {
      this.formData.cover_url = data.url
    },

    submit() {
      this.$refs.formRef.validate(async valid => {
        if (!valid) {
          this.$message.error('请完善必填项')
        }
        let res = {}
        if (this.id !== 0) {
          res = await updateTopic(this.formData, this.id)
        } else {
          res = await createTopic(this.formData)
        }

        if (res.code === 1) {
          this.$message.success(res.msg)
          this.formVisible = false
          this.getDataList()
        } else {
          this.$message.error(res.msg)
        }
        console.log(res)
      })
    },
    async del(row) {
      console.log(row)
      this.$confirm('是否确认删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await deleteTopic(row.id)

          if (res.code === 1) {
            this.getDataList()
            this.$message.success(res.msg)
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch(res => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style scoped>
.video_img img {
  width: 50px;
}
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 10px;
  margin: 10px;
}
</style>
